package com.zx.idc.backend.gui.base.house.service;

import com.zx.idc.backend.gui.api.commom.entity.UploadModel;
import com.zx.idc.backend.gui.api.commom.template.NIdcFtpTemplate;
import com.zx.idc.backend.gui.api.management.basicdata.BaseIdcBasicDataService;
import com.zx.idc.backend.gui.api.management.basicdata.report.IdcBasicDataReportService;
import com.zx.idc.backend.gui.common.base.BaseReportService;
import com.zx.idc.common.attributes.FTPReportDataTypeEnum;
import com.zx.idc.common.attributes.NetCommunicationFileTypeEnum;
import com.zx.idc.common.attributes.SchemaFileNameEnum;
import com.zx.idc.ds.base.house.service.IBaseHouseOfficerService;
import com.zx.idc.ds.common.constant.ReportStatusConstant;
import com.zx.idc.xml.entity.basedata.BasicInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author rxliuli
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BaseHouseOfficerReportService implements BaseReportService<Long> {
    @Autowired
    private IBaseHouseOfficerService service;
    @Autowired
    private NIdcFtpTemplate nIdcFtpTemplate;
    @Autowired
    private IdcBasicDataReportService idcBasicDataReportService;
    @Autowired
    private BaseIdcBasicDataService baseIdcBasicDataService;

    @Override
    public boolean insertReport(Long id) {
        return activeReporting(idcBasicDataReportService.baseHouseOfficerAdd(id));
    }

    @Override
    public boolean updateReport(Long id) {
        return activeReporting(idcBasicDataReportService.baseHouseOfficerUpdate(id));
    }

    @Override
    public boolean deleteReport(Long id) {
        return activeReporting(idcBasicDataReportService.baseHouseOfficerDelete(id));
    }

    private boolean activeReporting(BasicInfo basicInfo) {
        baseIdcBasicDataService.update(basicInfo, ReportStatusConstant.REPORTING);
        nIdcFtpTemplate.uploadXMLFile(
            new UploadModel<>(
                basicInfo,
                SchemaFileNameEnum.BASE_DATA_REPORT,
                FTPReportDataTypeEnum.FTP_BASE_DATA,
                NetCommunicationFileTypeEnum.BASE_DATA
            )
        );
        return true;
    }
}
